package ytdp.careagle.com.ytdp.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.Operation;
import ytdp.careagle.com.ytdp.callback.MyDialogCallback;
import ytdp.careagle.com.ytdp.dialog.CustomProgress;
import ytdp.careagle.com.ytdp.utils.MyToast;

/**
 * Created by Administrator on 2017/4/13 0013.
 */

public abstract class BaseFragment extends Fragment {
    private Operation mBaseOperation = null;
    private Toolbar toolbar;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
//		setTranslucentStatus();
        mBaseOperation = new Operation(getActivity());
    }

    protected void initToolbar(View view, String title) {
        toolbar = (Toolbar) view.findViewById(R.id.base_toolbar);
        if (toolbar == null) {
            return;
        }
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        toolbarTitle.setText(title);
    }

    protected void initToolbar(View view, String title, int rightId) {
        toolbar = (Toolbar) view.findViewById(R.id.base_toolbar);
        if (toolbar == null) {
            return;
        }
        TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.tv_title);
        toolbarTitle.setText(title);
        ImageView ivRight = (ImageView) toolbar.findViewById(R.id.iv_toolbar_right);
        ivRight.setImageResource(rightId);
        ivRight.setVisibility(View.VISIBLE);
    }

    public void showMyDialog(String msg, final MyDialogCallback callback) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示");
        builder.setMessage(msg);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callback.onClickOk(dialogInterface, i);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.create().show();
    }
//	/**
//	 * 设置状态栏背景状态
//	 */
//	@SuppressLint("InlinedApi")
//	private void setTranslucentStatus() {
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//			Window win = getActivity().getWindow();
//			WindowManager.LayoutParams winParams = win.getAttributes();
//			final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
//			winParams.flags |= bits;
//			win.setAttributes(winParams);
//		}
//		SystemStatusManager tintManager = new SystemStatusManager(
//				this.getActivity());
//		tintManager.setStatusBarTintEnabled(true);
//		tintManager.setStatusBarTintResource(0);// 状态栏无背景
//	}

    /**
     * 获取共通操作机能
     */
    public Operation getOperation() {
        return this.mBaseOperation;
    }


    public void showMessage(String str) {
        if (!TextUtils.isEmpty(str)) {
            MyToast.toast(str);
        }
    }

    private CustomProgress progressDialog;

    public void showProgress(int messageResId) {
        showProgress(getString(messageResId));
    }

    public void showProgress(String message, boolean b) {
        showProgress(message, false);
    }

    public void showProgress(String message) {
        progressDialog = CustomProgress.show(getActivity(), message, true, null);
    }

    public void hideProgress() {
        if (progressDialog != null) {
            try {
                progressDialog.dismiss();
                progressDialog = null;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
